package com.thti.mecool.client.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.command.send.plan.PlanSend;
import com.thti.mecool.client.model.biz.PlanBizImpl;
import com.thti.mecool.client.model.biz.TaskBizImpl;
import com.thti.mecool.client.model.po.PlanClient;
import com.thti.mecool.client.model.po.Task;
import com.thti.mecool.client.widget.ListWidget;
import com.thti.mecool.client.widget.ProgressDialogWidget;
import com.thti.mecool.client.widget.Toolbar;

public class MyPlanTaskActivity extends BaseActivity{
	TextView title,reason;
	PlanBizImpl planBizImpl;
	LinearLayout reasonLinearLayout;
	ProgressDialogWidget pd;
	ListWidget listWidget;
	private ArrayList<ArrayList<HashMap<String, String>>> taskList;
	private ArrayList<HashMap<String, String>> projects;
	ArrayList<String> listTag;
	private ArrayList<String> taskids, projectids;
	TaskBizImpl taskBizImpl;
	List<Task> tasks;
	Handler handler;
	String planId;
	@Override
	protected void onCreate(Bundle savedInstanceState) { 
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Cache.add(this);
		this.noTitle();
		setContentView(R.layout.mecool_myplan);
		initToolbar();
		planId=getIntent().getStringExtra("planId");
		init();
		PlanSend.getMyPlanTask(planId);
		handler.post(r);
		getDataFromDb();
	}
	
	private Runnable r=new Runnable() {
		@Override
		public void run() {
			if(getDataFlag(HandlerConstant.MyPlanJobCommand)){
				handler.removeCallbacks(r);
				removeDataFlag(HandlerConstant.MyPlanJobCommand);
				if(pd.isShowing())
					pd.dismiss();
				getDataFromDb();
			}else{
				if(getDataFlag(HandlerConstant.CommandFail)){
					handler.removeCallbacks(r);
					removeDataFlag(HandlerConstant.CommandFail);
					if(pd.isShowing())
						if(getCurrentMesg()!=null)
							if(getCurrentMesg().getContent()!=null)
						pd.dismiss(getCurrentMesg().getContent());
						else{
							pd.dismiss("获取信息失败！");
						}
				}else
					handler.postDelayed(r, 1000);
			}
		}
	};
	
	private void init(){
		handler=new Handler();
		reasonLinearLayout=(LinearLayout)findViewById(R.id.mecool_myplan_reason_linearlayout);
		title=(TextView)findViewById(R.id.mecool_myplan_plan_title);
		reason=(TextView)findViewById(R.id.mecool_myplan_reason);
		taskList=new ArrayList<ArrayList<HashMap<String,String>>>();
		projects=new ArrayList<HashMap<String,String>>();
		listTag = new ArrayList<String>();
		taskids = new ArrayList<String>();
		projectids = new ArrayList<String>();
		pd=new ProgressDialogWidget(this);
		planBizImpl=new PlanBizImpl(this);
		taskBizImpl=new TaskBizImpl(this);
	}
	
	public void initToolbar(){
		Toolbar toolbar=new Toolbar(this);
		toolbar.setToolbarCentreText("我的计划");
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
		Toolbar.right_btn.setOnClickListener(rightbuttonListener);
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
		Toolbar.right_btn.setImageResource(R.drawable.mecool_plan_edit);
	}
	
	private void getDataFromDb() {
		PlanClient planClient=planBizImpl.findByType(2,planId,String.valueOf(getCurrentUser().getId()));
		System.out.println(planId);
		if(planClient.getStatus().equals("confirm")){
			reasonLinearLayout.setVisibility(View.GONE);
			title.setText("您的计划审核通过");
		}else if(planClient.getStatus().equals("deny")){
			reasonLinearLayout.setVisibility(View.VISIBLE);
			title.setText("您的计划被退回");
			reason.setText(planClient.getDenyNotes());
		}else if(planClient.getStatus().equals("new")){
			reasonLinearLayout.setVisibility(View.GONE);
			title.setText("计划正在审核中");
		}else if(planClient.getStatus().equals("change")){
			reasonLinearLayout.setVisibility(View.GONE);
			title.setText("计划已变更");
		}
		tasks=taskBizImpl.findByPlan(planId,String.valueOf(getCurrentUser().getId()));
		taskList.clear();
		listTag.clear();
		projects.clear();
		
		for (Task task : tasks) {
			HashMap<String, String> Task = new HashMap<String, String>();
			projects = new ArrayList<HashMap<String, String>>();
			Task.put("name", task.getTaskName());
			Task.put("time", task.getStartTime() + " - " + task.getEndTime());
			Task.put("addr", task.getStoreName());
			taskids.add(String.valueOf(task.getId()));
			projectids.add(task.getProjectId());
			listTag.add(task.getProjectName());
			projects.add(Task);
			taskList.add(projects);
		}
		if(listWidget!=null)
			listWidget.OtherListRefurbish();
		else
		listWidget= new ListWidget(MyPlanTaskActivity.this, taskList, listTag);
	}
	
	private OnClickListener leftbuttonListener=new ImageButton.OnClickListener(){
		@Override 
		public void onClick(View v) { 
			finish();
		}};
		
	private OnClickListener rightbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(tasks!=null){
				Intent intent=new Intent(MyPlanTaskActivity.this,ChangePlanActivity.class);
				intent.putExtra("planid", planId);
				intent.putExtra("isMyPlan", true);
				startActivity(intent);
				finish();
			}
		}
		
	};
@Override
protected void onResume() {
	super.onResume();
};
}
